package com.serviceprovider.business.module.admin.license.services.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.license.model.LicenseRoleApply;
import com.serviceprovider.business.module.admin.license.services.dao.interfaces.ILicenseRoleApplyService;
import com.serviceprovider.business.utility.AppUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Service;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("licenseRoleApplyService")
public class LicenseRoleApplyServiceImpl extends JdbcDaoSupport implements ILicenseRoleApplyService {
    @Autowired
    public LicenseRoleApplyServiceImpl(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    @Override
    public List<LicenseRoleApply> findAll() {
        String sql =
            "select id,license_id,role_id,silent_creation,silent_update,announce_link_on_news_page,mandatory_at_new_registration,mandatory_at_next_login,mandatory_before_next_order from adm_license_role_apply";    // $NON-NLS-1$
        List                   list = getJdbcTemplate().queryForList(sql);
        List<LicenseRoleApply> ret  = new ArrayList<LicenseRoleApply>();
        Map                    row;
        LicenseRoleApply       aux;

        for (Object obj : list) {
            row = (Map) obj;
            aux = new LicenseRoleApply();
            aux.setId(new Integer(row.get("id").toString()));    // $NON-NLS-1$
            aux.setLicenseId(new Integer(row.get("license_id").toString()));
            aux.setRoleId(new Integer(row.get("role_id").toString()));
            aux.setSilentCreation(AppUtil.getBoolean(row.get("silent_creation")));
            aux.setSilentUpdate(AppUtil.getBoolean(row.get("silent_update")));
            aux.setAnnounceLinkOnNewsPage(AppUtil.getBoolean(row.get("announce_link_on_news_page")));
            aux.setMandatoryAtNewRegistration(AppUtil.getBoolean(row.get("mandatory_at_new_registration")));
            aux.setMandatoryAtNextLogin(AppUtil.getBoolean(row.get("mandatory_at_next_login")));
            aux.setMandatoryBeforeNextOrder(AppUtil.getBoolean(row.get("mandatory_before_next_order")));
            ret.add(aux);
        }

        return ret;
    }

    @Override
    public void insertLicenseRoleApply(LicenseRoleApply l) {
        final String sql =
            "insert into adm_license_role_apply (license_id,role_id,silent_creation,silent_update,announce_link_on_news_page,mandatory_at_new_registration,mandatory_at_next_login,mandatory_before_next_order) values(?,?,?,?,?,?,?,?)";    // $NON-NLS-1$

        getJdbcTemplate().update(sql, new Object[] {
            l.getLicenseId(), l.getRoleId(), l.isSilentCreation(), l.isSilentUpdate(), l.isAnnounceLinkOnNewsPage(),
            l.isMandatoryAtNewRegistration(), l.isMandatoryAtNextLogin(), l.isMandatoryBeforeNextOrder()
        });
    }

    @Override
    public void updateLicenseRoleApply(LicenseRoleApply l) {
        final String sql =
            "update adm_license_role_apply set license_id = ? , role_id = ?, silent_creation = ?, silent_update = ? ,announce_link_on_news_page = ? , mandatory_at_new_registration = ? ,mandatory_at_next_login = ? , mandatory_before_next_order = ? where license_id = ? and role_id = ?";    // $NON-NLS-1$

        getJdbcTemplate().update(sql, new Object[] {
            l.getLicenseId(), l.getRoleId(), l.isSilentCreation(), l.isSilentUpdate(), l.isAnnounceLinkOnNewsPage(),
            l.isMandatoryAtNewRegistration(), l.isMandatoryAtNextLogin(), l.isMandatoryBeforeNextOrder(),
            l.getLicenseId(), l.getRoleId()
        });
    }

    protected boolean isLicenseApplied(int licenseId, int roleId) {
        final String sql = "select count(*) from adm_license_role_apply where license_id = ? and role_id = ?";

        return getJdbcTemplate().queryForInt(sql, new Object[] { licenseId, roleId }) > 0;
    }

    @Override
    public void save(List<LicenseRoleApply> list) {
        for (LicenseRoleApply l : list) {
            if (!isLicenseApplied(l.getLicenseId(), l.getRoleId())) {    // we expect id = 0 for new records
                insertLicenseRoleApply(l);
            } else {
                updateLicenseRoleApply(l);
            }
        }
    }
}



